package com.kust.smarhome.background.controller;

import cn.hutool.core.map.MapUtil;
import com.google.code.kaptcha.Producer;
import com.kust.smarhome.background.common.lang.Const;
import com.kust.smarhome.background.common.lang.Result;
import com.kust.smarhome.background.utils.RedisUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
public class SystemController extends BaseController {


    @Value("file:Z:/upload")
    private File resourcePath;

    @Value("http://xiangrikui.resource.vaiwan.com")
    private String resourceHost;

    @PostMapping("/upload/images")
    /*
        将轮播图上传到//10.1.188.49/upload中的banner文件夹中
     */
    public Result uploadFile(MultipartFile[] fileList) throws IOException {

        System.out.println(fileList);

        List<String> urls = new ArrayList<>();

        String path = DateTimeFormatter.ofPattern("yyyy/MM/dd").format(LocalDate.now());
        for (MultipartFile imageFile : fileList) {
            File folder = new File(resourcePath,path);
            folder.mkdirs();

            String fileName = imageFile.getOriginalFilename();
            String ext = fileName.substring(fileName.lastIndexOf("."));
            String name = UUID.randomUUID().toString() + ext;

            File file = new File(folder, name);
            imageFile.transferTo(file);

            String url = resourceHost + "/" + name;
            urls.add(url);

        }

        return Result.succ(urls);
    }

    @Resource
    Producer producer;

    @Autowired(required = false)
    RedisUtil redisUtil;

    @GetMapping("/captcha")
    @ApiOperation("获取图片验证码")
    public Result captcha() throws IOException {
        String key = UUID.randomUUID().toString();
        String code = producer.createText();

        BufferedImage image = producer.createImage(code);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", outputStream);

        BASE64Encoder encoder = new BASE64Encoder();
        String str = "data:image/jpeg;base64,";

        String base64Img = str + encoder.encode(outputStream.toByteArray());

        redisUtil.hset(Const.CAPTCHA_KEY, key, code, 120);

        return Result.succ(
                MapUtil.builder()
                        .put("token", key)
                        .put("captchaImg", base64Img)
                        .build()
        );

    }
}
